#include"menu.h"

int welcome_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf("欢迎登陆设备管理系统\t\n输入 1 进入登陆界面输入 Q 退出: ");
		char temp;
		temp = getchar();
		while(getchar() != '\n');
		if('1' == temp)
			login_menu(devlist);
		else if( 'Q' == temp || 'q' == temp)
			break;
			
	}

	return 0;
}

//提示管理员输入密码与用户名
int login_menu(node_t *devlist)
{
	int count = 0;
	char Name[8];
	char Pass[8];
	while(count++ < LOOPCOUNT)
	{
		system("clear");
		printf("请输入管理员帐号：");
		scanf("%8s",Name);
		while(getchar() != '\n');

		printf("请输入管理员密码：");
		scanf("%8s",Pass);
		while(getchar() != '\n');
	
		if(0 == strcmp(Name,ADMIN_NAME) && 0 == strcmp(Pass,ADMIN_PASS))
		{
			admin_menu(devlist);
			return 0;
		}
		else
		{
			printf("还与 %d 次输入登陆机会 按任意按键继续\n",LOOPCOUNT-count);
			getchar();
		}
	}

		printf("登陆失败 输入任意按键退出\n");		
		getchar();
		return -1;

}

//管理员操作菜单
int admin_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf(" 1 进入添加菜单\t\n 2 进入删除菜单\t\n 3 进入修改菜单\t\n 4 进入显示菜单\t\n 5 进入查找菜单\t\n 0 返回上一级\t\n");
		printf("-------------------分割线----------------\n");
		printf("请输入一个数 :");
		
		char mode = getchar();
		while(getchar() != '\n');

		switch(mode)
		{
			case ADD :

				add_dev_menu(devlist);
				break;

			case DELE :

				delete_dev_menu(devlist);
				break;

			case UPDATA :

				update_dev_menu(devlist);
				break;

			case SHOW :
				
				show_dev_menu(devlist);
				break;

			case FIND :
				
				find_dev_menu(devlist);
				break;

			case EXIT : 
				return 0;
				break;
			default:
				printf("按任意ENTER键重新输入");
				while(getchar() != '\n');
				break;
		}
		
	}

}


//添加设备菜单
int add_dev_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");

		device_t newdev = {0};
		printf("请输入设备序号：");
		scanf("%d",&newdev.dev_order);
		while(getchar() != '\n');

		printf("请输设置设备工作状态 0 or 1 ：");
		scanf("%d",&newdev.dev_status);
		while(getchar() != '\n');

		printf("请输入设备当前值：");
		scanf("%f",&newdev.cur_value);
		while(getchar() != '\n');

		printf("请输入设备功率：");
		scanf("%f",&newdev.power_ele);
		while(getchar() != '\n');

		printf("请输入设备名：");
		scanf("%s",newdev.dev_name);
		while(getchar() != '\n');

		printf("请输入设备类型：");
		scanf("%s",newdev.dev_type);
		while(getchar() != '\n');

		printf("请输入设备IP：");
		scanf("%s",newdev.dev_ip);
		while(getchar() != '\n');

		int rel =  add_dev(devlist, &newdev);
		if( 0 == rel)
		{
			show_dev(devlist,showdev_all);
			printf("输入任意值回车返回\n");
			getchar();
			return 0;
		}
		else
		{
			printf("设备添加失败\n");
			printf("输入任意值回车返回\n");
			getchar();
			return -1;
		}
 		
	}
	
}


//删除设备
int delete_dev_menu(node_t *devlist)
{
		while(1)
	{
		system("clear");
		printf("当前设备\n");
		printf("--------------分割线-------------\n");
		show_dev(devlist,showdev_all);
		printf("--------------分割线-------------\n");

		printf("1 按设备号删除设备\t\n2 按设备名删除设备\t\n3 按类型删除设备\t\nq 返回上级\t\n");
		printf("--------------分割线-------------\n");
		printf("请输入数字 ：");

		device_t del_devdata = {0};
		int flag = 1;
		char mode = getchar();
		while(getchar() != '\n');

		switch(mode)
		{
			case '1' :

				 printf("请输入设备要删除的设备号：");
				 scanf("%d",&del_devdata.dev_id);
				 while(getchar() != '\n');
				 flag = del_dev(devlist,&del_devdata,cmpDviceid);
				break;

			case '2' :

				 printf("请输入设备要删除的设备名：");
				 scanf("%s",del_devdata.dev_name);
				 while(getchar() != '\n');
				 flag = del_dev(devlist,&del_devdata,cmpDvicename);
				break;

			case '3' :

				printf("请输入设备要删除的设备类型：");
				 scanf("%s",del_devdata.dev_type);
				 while(getchar() != '\n');
				 flag = del_dev(devlist,&del_devdata,cmpDvicetype);
				break;

			case 'q' :
			case 'Q' :

				return 0;

		}

		memset(&del_devdata,0,sizeof(device_t));
		if(!flag)
		{
			printf("删除成功\n");
			printf("\n");
			show_dev(devlist,showdev_all);
			printf("\n");
			printf("按任意按键继续: ");
			getchar();

		}
		else
		{
			printf("删除失败  按任意按键继续\n");
			getchar();
		}

	}
}


//更新设备菜单
int update_dev_menu(node_t *devlist)
{
		while(1)
	{
		system("clear");
		printf("当前设备\n");
		printf("--------------分割线-------------\n");
		show_dev(devlist,showdev_all);
		printf("--------------分割线-------------\n");
		printf("1 修改设备名\t\n2 修改设备类型\t\n3 修改设备IP\t\nq 返回上级\t\n");
		printf("请输入数字 ：");

		device_t up_devdata = {0};
		node_t *temp = NULL;
		int id = 0;
		char mode = getchar();
		while(getchar() != '\n');
		switch(mode)
		{
			case '1' :

				 printf("请输入要更改的设备号：");
				 scanf("%d",&id);
				 while(getchar() != '\n');
				 printf("请输入新设备名：");

				 scanf("%32s",up_devdata.dev_name);
				 while(getchar() != '\n');
				 temp = update_dev(devlist,id,&up_devdata,updatedev_name);
				 break;

			case '2' :

				 printf("请输入要更改的设备号：");
				 scanf("%d",&id);
				 while(getchar() != '\n');
				 printf("请输入新设备类型：");

				 scanf("%32s",up_devdata.dev_type);
				 while(getchar() != '\n');
				 temp = update_dev(devlist,id,&up_devdata,updatedev_type);				 
				 break;

			case '3' :

				 printf("请输入要更改的设备号：");
				 scanf("%d",&id);
				 while(getchar() != '\n');
				 printf("请输入新设备IP：");

				 scanf("%32s",up_devdata.dev_ip);
				 while(getchar() != '\n');
				 temp = update_dev(devlist,id,&up_devdata,updatedev_ip);	
				 break;

			case 'q' :
			case 'Q' :

				return 0;

		}

		if(NULL == temp)
		{
			printf("修改失败 按ENTER按键继续\n");
			while(getchar() != '\n')
				;
			
		}
		else
		{
			 printf("修改后的设备数据：\n");
			 showdev_all(temp->data);
			 printf("按任意按键继续：");
			 while(getchar() != '\n' )
				 ;
		}

	}

}


//显示设备菜单
int show_dev_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf("当前设备\n");
		printf("--------------分割线-------------\n");
		show_dev(devlist,showdev_all);
		printf("--------------分割线-------------\n");

		printf("1 设备号排序显示\t\n2 按设备名排序显示\t\n3 设备类型排序显示\t\nq 返回上级\t\n");
		
		printf("请输入数字 ：");
		char mode = getchar();
		while(getchar() != '\n');

		switch(mode)
		{
			case '1' :

					sort_dev(devlist,cmpDviceid);
					printf("----------分割线----------\n");
					show_dev(devlist, showdev_all);
					printf("----------分割线----------\n");
					printf("按任意按键继续:");
					getchar();
				 break;

			case '2' :

					sort_dev(devlist,cmpDvicename);
					printf("----------分割线----------\n");
					show_dev(devlist, showdev_all);
					printf("----------分割线----------\n");
					printf("按任意按键继续:");
					getchar();
				 break;

			case '3' :

					sort_dev(devlist,cmpDvicetype);
					printf("----------分割线----------\n");
					show_dev(devlist, showdev_all);
					printf("----------分割线----------\n");
					printf("按任意按键继续:");
					getchar();
				
				 break;

			case 'q' :
			case 'Q' :

				return 0;

		}

	}
}

int find_dev_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf("1 按设备号查找\t\n2 按设备名查找\t\n3 设备类型查找\t\nq 返回上级\t\n");
		
		node_t *find_devlist = NULL;
		device_t temp = {0};

		printf("请输入数字 ：");
		char mode = getchar();
		while(getchar() != '\n');

	switch(mode)
		{
			case '1' :

				printf("请输入你的设备号： ");
				scanf("%d",&temp.dev_id);
				while(getchar() != '\n' )
					;

				find_devlist = find_dev(devlist,&temp,cmpDviceid);
				break;

			case '2' :

				printf("请输入你的设备名： ");
				scanf("%32s",temp.dev_name);
				while(getchar() != '\n' )
					;

				find_devlist = find_dev(devlist,&temp,cmpDvicename);
				break;

			case '3' :

				printf("请输入你的设备类型： ");
				scanf("%32s",temp.dev_type);
				while(getchar() != '\n' )
					;

				find_devlist = find_dev(devlist,&temp,cmpDvicetype);		
				 break;

			case 'q' :
			case 'Q' :

				return 0;

		}

		if( NULL == find_devlist)
		{
			printf("没有找到该设备 按任意按键继续\n");
			while(getchar() != '\n');
		}
		else
		{
			printf("----------分割线----------\n");
			show_dev(find_devlist, showdev_all);
			printf("按任意按键继续\n");
			while(getchar() != '\n');

		}

		destroy_dlist(&find_devlist);

		}
}




